<template>
  <section class="common-container">
    <swiper class="swiper-container" :slides-per-view="slidesPerView" :space-between="spaceBetween" @swiper="onSwiper"
      @slideChange="onSlideChange" :modules="modules" :pagination="pagination" :autoplay="{
        delay: 5000,
        disableOnInteraction: false
      }" :navigation="navigation" :loop="loop">
      <swiper-slide class="swiper-item" v-for="(item, index) in options" :key="index">
        <!-- 根据媒体类型显示图片或视频 -->
        <template v-if="isVideo(item.image)">
          <video ref="videoRef" class="video-player" :src="item.image" muted autoplay :controls="true"
            :playsinline="true" width="100%" height="100vh"></video>
        </template>
        <template v-else>
          <img class="item" :src="item.image" alt="media content" />
        </template>
        <div class="item-info">
          <!-- <div class="item-name" :style="{ 'right':index===1? '10%' : 0, color: index===0 ? '#fff' : '#000' }">{{ item.title }}</div> -->
          <!-- <a :href="`/product/${item.url || ''}.html`" target="_self" :title="item.title" class="shop-now" :style="{ 'right':index===1? '10%' : 0 }">Shop Now</a> -->
        </div>
      </swiper-slide>
    </swiper>
  </section>

</template>
<script lang="ts" setup>
import { ref, getCurrentInstance } from 'vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Navigation, Pagination, A11y, Autoplay } from 'swiper/modules';
import VideoPlayer from '@/components/VideoPlayer.vue';
// @ts-ignore - Ignore CSS imports for TypeScript
import 'swiper/css';
// @ts-ignore
import 'swiper/css/navigation';
// @ts-ignore
import 'swiper/css/pagination';
// @ts-ignore
import 'swiper/css/autoplay';
import { useRouter, useRoute } from 'vue-router';
// @ts-ignore
import { useCurrencyStore } from '@/store';

// @ts-ignore - Ignore proxy property access
const { proxy } = getCurrentInstance();
const useCurrency = useCurrencyStore();
const router = useRouter();

// Define media item interface
interface MediaItem {
  title?: string;
  image?: string;
  url?: string;
  classify_id?: string;
  [key: string]: any;
}

// 检查是否是视频文件
const isVideo = (src?: string): boolean => {
  if (!src) return false;
  const videoExtensions = ['.mp4', '.webm', '.ogg', '.mov', '.avi'];
  return videoExtensions.some(ext => src.toLowerCase().endsWith(ext));
};

// 获取视频类型
const getVideoType = (src?: string): string => {
  if (!src) return 'video/mp4';
  if (src.toLowerCase().endsWith('.mp4')) return 'video/mp4';
  if (src.toLowerCase().endsWith('.webm')) return 'video/webm';
  if (src.toLowerCase().endsWith('.ogg')) return 'video/ogg';
  if (src.toLowerCase().endsWith('.mov')) return 'video/quicktime';
  if (src.toLowerCase().endsWith('.avi')) return 'video/x-msvideo';
  return 'video/mp4'; // 默认返回 mp4
};

const props = defineProps({
  //轮播列表
  options: {
    type: Array as () => MediaItem[],
    default: () => ([])
  },
  // 轮播一行显示图片个数
  slidesPerView: {
    type: Number,
    default: 3
  },
  // 轮播间隔
  spaceBetween: {
    type: Number,
    default: 20
  },
  // 自动播放
  autoplay: {
    type: Boolean,
    default: true
  },
  // 是否循环播放
  loop: {
    type: Boolean,
    default: true
  },
  // 是否显示分页器
  pagination: {
    type: Boolean,
    default: true
  },
  // 是否显示导航按钮
  navigation: {
    type: Boolean,
    default: true
  },
  // 标题
  title: {
    type: String,
    default: 'TITLE'
  }
});

const emits = defineEmits(['handleCarouselItemClick', 'handleClearHistory'])

const modules = [Navigation, Pagination, A11y, Autoplay]
const swiperInstance = ref<any>(null);

const onSwiper = (swiper: any) => {
  swiperInstance.value = swiper;
};
const onSlideChange = () => {
  console.log('slide change');
};

const handleClick = (item: MediaItem) => {
  window.location.href = `/product/${item.url || ''}.html`
}

const handleClearHistory = () => {
  emits('handleClearHistory')
}
</script>

<style lang="scss" scoped>
.swiper-container {
  background-color: transparent;
  position: relative;
}

.common-container {
  // padding: 60px 0;

  .carousel-header {
    margin-bottom: 24px;

    .title {
      font-size: 16px;
      font-weight: bold;
      letter-spacing: 0.5px;
      margin: 0;
    }

    .button {
      font-size: 12px;
      color: #000;
      background: none;
      border: none;
      padding: 0;
      cursor: pointer;
      text-decoration: underline;
      font-weight: bold;
    }
  }
}

.item {
  width: 100%;
  height: 100%;

  &.video-item {
    object-fit: cover;
    aspect-ratio: 4/3;
  }
}

.swiper-item {
  margin-right: 20px;
  text-align: left;
  position: relative;

  &:last-child {
    margin-right: 0;
  }

  img {
    width: 100%;
    height: 100%;
    // aspect-ratio: 4/2.5;
    object-fit: cover;
    margin-bottom: 12px;
  }

  video {
    width: 100%;
    height: 100%;
    // aspect-ratio: 4/3;
    object-fit: cover;
    margin-bottom: 12px;
    // background-color: #000;
  }
}

.item-info {
  position: absolute;
  bottom: 60%;
  right: 10%;

  .item-name {
    font-size: 36px;
    font-weight: bold;

    // color: #fff;
    margin-bottom: 4px;
    text-transform: uppercase;
  }

  .shop-now {
    // text-decoration: underline;
    font-size: 20px;
    border: 1px solid #000;
    padding: 10px 20px;
    margin-top: 20px;
    margin: 0 auto;
  }

  .item-description {
    font-size: 12px;
    color: #666;
    margin-bottom: 4px;
    line-height: 1.4;
  }

  .item-price {
    font-size: 12px;
    font-weight: 600;
  }
}

::v-deep(.swiper-container .swiper-button-prev, .swiper-rtl .swiper-button-next),
::v-deep(.swiper-container .swiper-button-next, .swiper-rtl .swiper-button-prev) {
  color: #fff !important;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  line-height: 36px;
  text-align: center;
  font-size: 12px;
  top: calc(50% - 12px);
  transform: translateY(-50%);
  transform: scale(3);

  &:after {
    font-size: 12px !important;
    animation: swiper-preloader 1s infinite ease-in-out;
  }
}

::v-deep(.swiper-container .swiper-button-next, .swiper-rtl .swiper-button-prev) {
  right: 0;
}

::v-deep(.swiper-container .swiper-button-prev, .swiper-rtl .swiper-button-next) {
  left: 0;
}
</style>
